<template>
	<view class="content" :style="{'padding-top':top+'px','min-height':windowheight+'px'}">
		<Navbar title="我的优惠券" bgColor="#da708b" :backIcon="false" style="color: #ffffff;z-index: 1;"></Navbar>
		<view class="coupon_tabs" :style="{'top':top+'px'}">
			<view v-for="(item,index) in coupontabs" :key="index" :class="currentTab===index?'coupon_tabs_act':''" @tap.stop="changeTab(index)">{{item.name}}</view>
		</view>
		<view class="coupon_list">
			<view v-if="coupons.length===0" style="position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);">无优惠券</view>
			<view class="coupon_item" v-for="(item,index) in coupons" :key="index">
				<view class="couponi_tit">
					<view class="couponit_amount" v-if="item.couponType ==='FAVORABLE'">￥{{item.couponFavorable}}</view>
					<view class="couponit_amount" v-else>{{item.couponDiscount*10}}折</view>
					<view class="couponit_condt">满{{item.amountFull}}元可用</view>
				</view>
				<view class="couponi_con">
					<view class="couponic_txt">
						<text class="couponict_tit">{{item.couponRuleName}}</text>
						<text class="couponict_mer">仅限在{{item.remark}}满足条件时使用</text>
						<uni-dateformat :date="item.expirationTime" format="有效期至yyyy-MM-dd" class="couponict_exp"></uni-dateformat>
					</view>
					<view class="couponic_btn" v-if="item.couponStatus=='UNUSED'">
						<button type="default" size="mini" @tap="use">去使用</button>
					</view>
					<view class="couponic_img" v-if="item.couponStatus=='FAILURE'">						
						<image src="http://zzqjt-test.oss-cn-hangzhou.aliyuncs.com/templates/client/res/6bb1367405b74a18808e3564e1e64970-expired.png"></image>
					</view>
					<view class="couponic_img" v-if="item.couponStatus=='USED'">
						<image src="http://zzqjt-test.oss-cn-hangzhou.aliyuncs.com/templates/client/res/66b9eda3fe5e49a0a5031ddb0c1d1913-haveBeenUsed.png"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script setup>
	import { ref,onMounted } from 'vue'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const baseUrl = getApp().globalData.baseUrl
	const title=ref('Hello')
	const windowheight=ref()
	const platform = ref()
	const statusBar = ref()
	const customBar = ref()
	const top = ref()
	const coupontabs=ref([
		{id:0,name:"未使用",value:"UNUSED"},
		{id:1,name:"已使用",value:"USED"},
		{id:2,name:"已失效",value:"FAILURE"}
	])
	const currentTab=ref(0)
	const coupons=ref([])
	//Tabs切换
	const changeTab=(index)=>{
		currentTab.value=index
		getcoupons(coupontabs.value[index].value)
	}
	const use=()=>{
		uni.navigateTo({
			url: "/pages/myalbum/myalbum"
		})
	}
	//获取优惠券信息
	const getcoupons=(couponStatus)=>{
		console.log(couponStatus)
		console.log(JSON.parse(uni.getStorageSync("user_info")).client_no)
		uni.request({
			url: getApp().globalData.baseUrl + "/coupon/list",
			method: "POST",
			header: {
				'content-type': 'application/json',
				'Access-Control-Allow-Origin': "*",
				'Authorization': uni.getStorageSync('token')
			},
			data: {
				clientNo: JSON.parse(uni.getStorageSync("user_info")).client_no,
				couponStatus:couponStatus
			},
			success: (res) => {
				console.log(res)
				coupons.value=res.data.value
		
			},
			fail: (err) => {
				console.log(err)
			}
		})
	}
	const getColumnHeight=()=>{
		uni.getSystemInfo({
			success(res) {
				console.log(res)
				platform.value = res.platform
				windowheight.value=res.screenHeight
				// #ifdef MP-WEIXIN
				statusBar.value = res.statusBarHeight
				const custom = wx.getMenuButtonBoundingClientRect()
				customBar.value = custom.bottom + custom.top - res.statusBarHeight
				// #endif
				// #ifdef APP-PLUS
				statusBar.value = res.statusBarHeight
				customBar.value = res.statusBarHeight + 45
				// 这里是在安卓手机上加上 3 像素（当时好像是在安卓水滴屏上，系统导航栏高度较低才加上去的，大家可以真机自己调试一下）
				if (res.platform == "android") {
					statusBar.value += 3
					customBar.value += 3
				}
				// #endif
			}
		})
		top.value = customBar.value
		windowheight.value=windowheight.value-top.value
		//console.log(statusBar.value, customBar.value, top.value,windowheight.value)
	}
	onShow(()=>{
		const now = new Date().getTime()
		if (!uni.getStorageSync('token') || now > uni.getStorageSync('expireTime')) {
			uni.reLaunch({
				url: "/pages/login/login"
			})
		}else{
			uni.request({
				url: getApp().globalData.baseUrl + "/coupon/list",
				method: "POST",
				header: {
					'content-type': 'application/json',
					'Access-Control-Allow-Origin': "*",
					'Authorization': uni.getStorageSync('token')
				},
				data: {
					clientNo: JSON.parse(uni.getStorageSync("user_info")).client_no,
					couponStatus:"UNUSED"
				},
				success: (res) => {
					console.log(res)
					coupons.value=res.data.value
			
				},
				fail: (err) => {
					console.log(err)
				}
			})
		}
	})
	onMounted(()=>{
		getColumnHeight()
	})
</script>
<style lang="less">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		//background-image: linear-gradient(to bottom,#d95e7d,#ffffff);
	}
	.coupon_tabs{
		z-index: 1;
		position:fixed;
		width: 100%;
		height:60rpx;
		background-color: #fff;
		padding:10rpx;
		display: flex;
		justify-content: space-around;
		border-bottom:2px solid #eee;
	}
	.coupon_tabs_act{
		border-bottom:2px solid #e77c90;
		color:#e77c90;
	}
	.coupon_list{
		padding-top: 90rpx;
	}
	.coupon_item{
		border:1px solid #ddd;
		height:150rpx;
		margin:10rpx;
		border-radius: 8rpx;
		display: flex;	
	}
	.couponi_tit{
		background-color: #fee3e4;
		width: 200rpx;
		padding:20rpx;
		text-align: center;
		color:#e77c90;
	}
	.couponi_con{
		background-color: #fff;
		padding:20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
	}
	.couponit_amount{
		font-size:32rpx;
		font-weight: bold;
		margin:10rpx 0;
	}
	.couponit_condt{
		font-size:24rpx;
	}
	.couponic_txt{
		font-size:24rpx;
		display: flex;
		flex-direction: column;
		line-height: 40rpx;
		
	}
	.couponict_tit{
		font-size: 30rpx;
	}
	.couponict_mer,.couponict_exp{
		color:#ccc;
	}
	.couponic_btn button{
		color:#fff;
		background-color: #e77c90;
		border-radius: 40rpx;
	}
	.couponic_img{
		width: 120rpx;
		height: 120rpx;
	}
	.couponic_img image{
		width: 100%;
		height: 100%;
	}
</style>